#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFile>
#include <QStandardItem>

#include "EmRhoModel.h"
#include "rhotablemodel.h"

#include <lib/QCustomPlot/qcustomplot.h>

void MainWindow::testTable()
{
    auto fileName = "d:/ed/苏热2井/2/视电阻率数据.dat";

    QStringList lines;
    QFile file(fileName);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream stream(&file);
        stream.setCodec("GBK"); // 设置编码为GBK
        while (!stream.atEnd())
        {
            QString line = stream.readLine();
            lines.append(line);
        }
        file.close();
    }

    RhoTableModel *rhoTableModel = new RhoTableModel(ui->tableView);

    QList<EmRhoModel> list;

    for (int i = 1; i < lines.size(); i++) {

        EmRhoModel model;

        QStringList items = lines[i].split(QRegExp("[, ]"), Qt::SkipEmptyParts);

        int index = 0;
        model.Line = items[index++].toInt();
        model.Point = items[index++].toDouble();
        model.DistancePoint = items[index++].toDouble();


        rhoTableModel->modelData.append(model);

        list.append(model);
    }

    rhoTableModel->setModelData(list);
    ui->tableView->setModel(rhoTableModel);
}

void MainWindow::testModel()
{
    QStandardItemModel* model = new QStandardItemModel();

    QStandardItem* item = new QStandardItem("root");
    QStandardItem* childItem1 = new QStandardItem("111");
    QStandardItem* childItem2 = new QStandardItem("222");

    item->appendRow(childItem1);
    item->appendRow(childItem2);

    QStandardItem* item2 = new QStandardItem("root2");

    model->appendRow(item);
    model->appendRow(item2);

    ui->treeView->setModel(model);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::testCode()
{
    auto n = 43;

    auto g = new QCPColorGradient(QCPColorGradient::GradientPreset::gpJet);
    g->setLevelCount(n);
    g->setPeriodic(true);

    for (int i = 0; i < n; i++) {
        QCPRange range(0, n);
        auto color = g->color(i, range);

        qDebug() << i << ", " << color;
    }
}

void MainWindow::testListWidget()
{
    QStringList labels;

    labels << "item1" << "item2" << "item3";

    ui->listWidget->addItems(labels);


    // connect(ui->listWidget, &QListWidget::itemChanged, this, [=](QModelIndex pos) {

    //     auto index = pos.row();
    //     qDebug() << "ui->listWidget index: " << index;

    // });


    connect(ui->listWidget, &QListWidget::currentItemChanged, this, [=](QListWidgetItem *current, QListWidgetItem *previous) {

        // auto index = pos.row();
        // qDebug() << "ui->listWidget index: " << index;

        auto index = ui->listWidget->row(current);

        qDebug() << index;
        qDebug() << current->text();

    });
}

void MainWindow::testDll()
{
    // 加载DLL文件
    QLibrary library("WFEM_1D_GS_x64"); // mydll为DLL文件名，不需要后缀名
    if (!library.load()) {
        qDebug() << "fail" << library.errorString();
        return ;
    }

    // 调用DLL中的函数
    typedef int (*WFEM_1D_GS)(int, int*, double *, double *, double *, double *, double *, double *, double *, double *, double *, double *); // 定义函数指针类型
    WFEM_1D_GS myFunction = reinterpret_cast<WFEM_1D_GS>(library.resolve("WFEM_1D_GS")); // myFunction为DLL中的函数名
    if (myFunction == nullptr) {
        qDebug() << "can't find the function " << library.errorString();
        library.unload();
        return ;
    }



    // int result = myFunction(1, 2); // 调用DLL中的函数
    // qDebug() << "函数返回值：" << result;
    // library.unload(); // 卸载DLL文件
    // qDebug() << "ok!";
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // testTable();
    // testModel();
    // testCode();
    // testListWidget();    
    // testDll();

    testCustomChart();

}

void MainWindow::testCustomChart()
{

    QString strx="0 0 -0.103892958838749 0.0231830026576785 -0.000642837747169889 0.0231801101946876 -0.080064807813043 0.162968182977877 -0.0133514281183733 0.174087760136475 0.237626169770124 0.261453116501848 0.171443483400444 0.250829427173352 0.261453116501848 0.274401220013896 0.275847650273527 0.343786126829038 0.277029782484097 0.273779600370653 0.261453116501848 0.281930221780238 0.32583294709543 0.282466301860318 0.272448903512369 0.286318620522645 0.29614908769648 0.269058400168447 0.292443777407464 0.296476165435194 0.310455190918913 0.29412072216826 0.283304298154878 0.284746222554253 0.265666487042249 0.25965862612621 0.261453116501848 0.239660680616157 0.223783157393665 0.239211791360852 0.163846360222356 0.224738540361032 0.182351676321721 0.172635293205101 -0.104773503485563 0.130575877095979 0.259059989388623 0.261453116501848 0.517330807212572 0.305012570352243 0.495147184027635 0.400459153902504 0.435949288704727";
    QString stry="3.78845120702346 3.70926996097583 3.48742121135947 3.40823996531185 3.18639121569549 3.10720996964787 2.88536122003151 2.80617997398389 2.58433122436753 2.50514997831991 2.28330122870355 2.20411998265592 1.98227123303957 1.90308998699194 1.80617997398389 1.7481880270062 1.68124123737559 1.60205999132796 1.50514997831991 1.44715803134222 1.38021124171161 1.30102999566398 1.20411998265592 1.14612803567824 1.07918124604762 1 0.903089986991944 0.845098040014257 0.778151250383644 0.698970004336019 0.602059991327962 0.544068044350276 0.477121254719662 0.397940008672038 0.301029995663981 0.243038048686294 0.176091259055681 0.0969100130080564 0 -0.0579919469776868 -0.1249387366083 -0.204119982655925 -0.301029995663981 -0.359021942641668 -0.602059991327962 -0.660051938305649 -0.903089986991944 -0.96108193396963 -1.20411998265592 -1.26210795895936 -1.50514997831991 -1.56313795462334 -1.80617997398389 ";

    // strx="0 -10 10 -10 10";
    // stry="0 10 20 30 40";

    QStringList arrTxt = strx.split(QRegExp("[ ]"), Qt::SkipEmptyParts);
    QList<double> dx; //点距
    foreach (QString key, arrTxt)
    {
     dx.append(key.toDouble());
    }
     QStringList arrTxt1 = stry.split(QRegExp("[ ]"), Qt::SkipEmptyParts);
     QList<double> dy; //点距
     foreach (QString key, arrTxt1)
     {
         dy.append((key.toDouble()));
     }

    QCustomPlot* customPlot = new QCustomPlot;
    QCPTextElement *plotTitle = new QCPTextElement(customPlot);
    plotTitle->setText("拟地震图");
    customPlot->plotLayout()->insertRow(0);
    customPlot->plotLayout()->addElement(0, 0, plotTitle);
    QCPScatterStyle *scatterStyle = new QCPScatterStyle(QCPScatterStyle::ScatterShape::ssDisc);
    // 设置点的大小
    scatterStyle->setSize(1); //

    customPlot->xAxis->setLabel("Station(m)");
    customPlot->yAxis->setLabel("lgf(Ω·m)");



    customPlot->addGraph();
    customPlot->graph(0)->setData(dx.toVector(),dy.toVector(),true);

    //customPlot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置
    // 将刻度刻度器添加到对应的轴上
    customPlot->xAxis->setSubTicks(false);



    // 创建0线的数据集
    QList<double> zeroLine;
    for (int i = 0; i < dx.size(); ++i) {
        zeroLine.append(0.1001);
    }

    // 添加0线作为辅助图
    customPlot->addGraph();
    customPlot->graph(1)->setData(zeroLine.toVector(), dy.toVector());
    customPlot->graph(1)->setVisible(false); // 隐藏0线


        
    // // 设置填充颜色
    // QBrush fillBrush(Qt::blue);
    // fillBrush.setStyle(Qt::SolidPattern);

    // // 设置第一个图的填充
    // customPlot->graph(0)->setBrush(fillBrush);


    // QLinearGradient gradient(0, 0, 1, 0); // 从左上到右下
    // gradient.setColorAt(0, Qt::blue);
    // gradient.setColorAt(1, Qt::green);

    // QBrush fillBrush(gradient);
    // customPlot->graph(0)->setBrush(fillBrush);

    // // 设置填充范围
    // customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));


    // // 设置渐变色
    // QLinearGradient gradient(0, 0, 1, 0); // 水平渐变，从左到右
    // gradient.setColorAt(0, Qt::blue);
    // gradient.setColorAt(1, Qt::green);

    // QBrush fillBrush(gradient);
    // customPlot->graph(0)->setBrush(fillBrush);


    // 设置填充颜色
    QBrush fillBrush(Qt::blue);
    fillBrush.setStyle(Qt::SolidPattern);
    
    customPlot->graph(0)->setBrush(fillBrush);


    // 设置填充范围，只填充中线右边区域
    customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));






    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    customPlot->rescaleAxes(true);
    customPlot->replot();
    
    // 调试信息
    qDebug() << "Graph 0 data size:" << customPlot->graph(0)->data()->size();
    qDebug() << "Graph 1 data size:" << customPlot->graph(1)->data()->size();


    ui->verticalLayout->addWidget(customPlot);
}
